package com.jz.stockidentificationclass.service.impl;

import com.jz.stockidentificationclass.mapper.StockidentificationMapper;
import com.jz.stockidentificationclass.model.*;
import com.jz.stockidentificationclass.service.StockidentificationService;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class StockidentificationServiceImpl implements StockidentificationService {

    @Autowired
    private StockidentificationMapper stockidentificationMapper;

    @Override
    public ResultVo firstPage(StockidentificationSelectVo stockidentificationSelectVo) {
        List<StockidentificationVo> stockidentificationVoList = stockidentificationMapper.queryData(stockidentificationSelectVo);
        //查询数量
        for (StockidentificationVo stockidentificationVo : stockidentificationVoList) {

            int status = stockidentificationVo.getStatus() ? 1 : 0;
            stockidentificationVo.setStatusName(SubStatusEnum.getNameByStatus(status));
        }
        int count = stockidentificationMapper.queryCount(stockidentificationSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(stockidentificationVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(stockidentificationSelectVo.getPageIndex());
        allRecordsVo.setPageSize(stockidentificationSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addpartClass(StockidentificationAddVo partAddVo) {
        //验证编码是否存在
        StockidentificationVo stockidentificationVO = stockidentificationMapper.queryByCode(partAddVo.getCode());
        if (stockidentificationVO != null) {
            return ResultBuildVo.error("备件编码已经存在！");
        }
        //验证名称是否存在
        StockidentificationVo subClassName = stockidentificationMapper.queryByName(partAddVo.getName());
        if (subClassName != null) {
            return ResultBuildVo.error("备件名称已经存在！");
        }
        Date nowDate = new Date();
        StockidentificationVo classVo = new StockidentificationVo();
        BeanUtils.copyProperties(partAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        stockidentificationMapper.add(classVo);
        return ResultBuildVo.success();
    }

//
//    @Override
//    public ResultVo queryById(Integer id) {
//
//        CatVo partVo = stockidentificationMapper.queryById(id);
//
//        return ResultBuildVo.success(partVo);
//    }
//
    @Override
    public ResultVo updateSubClass(@Valid StockidentificationUpdateVo partUpdateVo) {
        List<StockidentificationVo> stockidentificationVo = stockidentificationMapper.queryById(partUpdateVo.getId());
        if (stockidentificationVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        StockidentificationVo subClassName = stockidentificationMapper.queryByName(partUpdateVo.getName());
        if (subClassName != null && partUpdateVo.getId() != subClassName.getId()) {

            return ResultBuildVo.error("修改名称已存在");
        }

        Date nowDate = new Date();
        StockidentificationVo classVo = new StockidentificationVo();
        BeanUtils.copyProperties(partUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
         stockidentificationMapper.update(classVo);
//        if (version == 0){
//
//            return ResultBuildVo.error("已被修改，刷新重试");
//        }
        return ResultBuildVo.success();

    }

//    @Override
//    public ResultVo queryByUserId(Integer userId) {
//
//       List<WarehouseareaVo> legalPersonVoLists = stockidentificationMapper.queryByUserId(userId);
//
//        return  ResultBuildVo.success(legalPersonVoLists);
//    }
//
    @Override
    public ResultVo updateStatus(@Valid StockidentificationUpdateStatusVo stockidentificationUpdateStatusVo) {
        List<StockidentificationVo> stockidentificationVo = stockidentificationMapper.queryById(stockidentificationUpdateStatusVo.getId());
        if (stockidentificationVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        StockidentificationVo classVo = new StockidentificationVo();
        BeanUtils.copyProperties(stockidentificationUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        stockidentificationMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }

}

